import os
from tkinter import Frame, Label
from tkinter import W

from CommonOperations.project_root_path import project_root_path
from CommonOperations.get_all_configs import get_all_configs
from CustomWidget.custom_button import CustomButtonPair
from CustomWidget.custom_label_pair import CustomLabelPair, CustomLabelCheckbox
from CustomWidget.custom_separator import CustomSeparator



class SettingFrame(Frame):
    """配置显示窗口"""
    def __init__(self, master):
        self.root = master
        self.root.config()
        self.root.title("设置")
        # self.root.geometry(f"500x400")

        super(SettingFrame, self).__init__(master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.version = CustomLabelPair(self, "版本", 1)
        self.createtime = CustomLabelPair(self, "更新时间", 2)
        CustomSeparator(self, 5)

        self.img_w = CustomLabelPair(self, "W 宽px", 6, 1, right_col=1)
        self.img_h = CustomLabelPair(self, "H 高px", 6, 3, right_col=1)
        self.img_type = CustomLabelCheckbox(self, "格式", 7, check_num=3)
        self.big_img_dir = CustomLabelPair(self, "图库路径", 8)
        self.save_img_dir = CustomLabelPair(self, "保存路径", 9)
        self.whether_add_border = CustomLabelCheckbox(self, "描边", 10, check_num=1)
        self.border_color = CustomLabelPair(self, "描边颜色", 11, right_col=1)
        self.border_color_show = Label(self, background="#FFFFFF", width=10)
        self.border_color_show.grid(row=11, column=3, sticky=W, columnspan=1)
        self.border_width = CustomLabelPair(self, "边宽px", 12)
        CustomSeparator(self, 20)

        self.font_size = CustomLabelPair(self, "文字大小", 21)
        self.font_color = CustomLabelPair(self, "文字颜色", 22, right_col=1)
        self.font_color_show = Label(self, background="#FFFFFF", width=10)
        self.font_color_show.grid(row=22, column=3, sticky=W, columnspan=1)
        self.whether_add_text = CustomLabelCheckbox(self, "文字水印", 23, check_num=1)
        CustomSeparator(self, 30)

        self.time_check = CustomLabelCheckbox(self, "文件名", 31, check_num=1)

        self.show_ini_config()
        lf = self.change_edit_setting_window
        rf = self.change_create_window
        self.buttons = CustomButtonPair(self, 40, "修改", lf, "返回", rf, pady=10)

    def show_ini_config(self):
        """显示ini文件中的配置情况"""
        all_ini_data = get_all_configs()
        # 将数据显示到label中
        self.version.fresh_data(all_ini_data['version'])
        self.createtime.fresh_data(all_ini_data['createtime'])

        self.img_w.fresh_data(all_ini_data['img_w'])
        self.img_h.fresh_data(all_ini_data['img_h'])
        self.img_type.fresh_data(all_ini_data['check_list'])
        project_root = project_root_path()
        big_path = os.path.join(project_root, all_ini_data['big_dir'])
        self.big_img_dir.fresh_data(big_path)
        save_path = os.path.join(project_root, all_ini_data['small_dir'])
        self.save_img_dir.fresh_data(save_path)
        self.whether_add_border.fresh_data(all_ini_data['whether_add_border'])
        self.border_width.fresh_data(all_ini_data['border_width'])
        self.border_color.fresh_data(all_ini_data['border_color'])
        self.border_color_show['background'] = all_ini_data['border_color']

        self.font_size.fresh_data(all_ini_data['font_size'])
        self.font_color.fresh_data(all_ini_data['font_color'])
        self.font_color_show['background'] = all_ini_data['font_color']
        self.whether_add_text.fresh_data(all_ini_data['whether_add_text'])

        self.time_check.fresh_data(all_ini_data['timestamp'])


    def change_edit_setting_window(self):
        """跳转修改页"""
        self.destroy()
        from PageFrames.setting_frame_edit import SettingFrameEdit

        SettingFrameEdit(self.root)

    def change_create_window(self):
        """返回创建图片页"""
        self.destroy()
        from PageFrames.create_new_img_frame import CreateNewImageFrame

        CreateNewImageFrame(self.root)